<?php
include '../include/functions.php';
//set session save path
session_save_path("../".$session_path);
// Initialize a session. 
session_start();
$count++;
$start = time();
$sessionId = session_id();
if((!$userID) || ($PlayAFL == 0))
{
	header("Location: schedule.php"); /* Redirect browser */
	/* Make sure that code below does not get executed when we redirect. */
	exit;
}
//include 'include/SurvivalPicks.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/MAIN.dwt.php" codeOutsideHTMLIsLocked="false" -->
  <head>
    <!-- InstanceBeginEditable name="doctitle" -->
    <title>JoshHughes.com AFL Football Pool</title>
    <!-- InstanceEndEditable -->
	<link rel="stylesheet" href="http://football.joshhughes.com/css/AFL.css" type="text/css">
    <script src="http://football.joshhughes.com/scripts/prototype.js" type="text/javascript"></script>
	<script src="http://football.joshhughes.com/scripts/scriptaculous.js" type="text/javascript"></script>
	<script src="http://football.joshhughes.com/scripts/football.js" type="text/javascript"></script>
    <!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<!-- InstanceBeginEditable name="Contents" -->
				<h3>Straight Pick 'Em Picks</h3>
				<p>Just pick who you think will win.  No spread to worry about.  One point for each correct pick.  Make your pick by selecting the team from the drop-down list at the right, or by clicking on the team logo.  Click the "Submit/Update" button to submit the changes.</p>
				<table width="510" cellpadding="5" cellspacing="0">
					<tr>
					<td class="redbg">
					<?php
					//Connect To Database
					dbConnect();
					
					//check to see if a Weeknum value was passed, if not, set WeekNum to CurrWeek.
					if (($Weeknum == NULL) || ($Weeknum == "")) {
						$Weeknum = getWeek("AFL");
					}
					
					//check to make sure $Weeknum > 1 AND $Weeknum < 21
					$Weeknum = checkWeekBounds($Weeknum);
					
					//if (($Weeknum > 17) && ($MadePlayoffs == 1))
					if ($Weeknum <= 17)
					{
						//Write SQL Query to extract specified data
						$SQL = "SELECT * FROM AFLSchedule Where Week=".$Weeknum." ORDER BY GameDate, GameTime, GameNum ASC";
						//Open Record Set using SQL Query
						$result = mysql_query($SQL) or die("Cannot query the database to get AFL scehdule data.<br/>");
						$numGames = mysql_num_rows($result);
						
						$UserPickedSQL = "SELECT * FROM AFLPicks WHERE (UserID = ".$userID.") AND (Week = ".$Weeknum.")";
						$UserPickedresult = mysql_query($UserPickedSQL) or die("Cannot query the database to see if user has made picks.<br/>");
						$UserPickedrow = mysql_fetch_array($UserPickedresult);
						
						?>
						<form action="<? echo $PHP_SELF; ?>" method="POST" name="WeekForm">
						<table bgcolor=#FFFFFF width="650" align="center"><tr>
						<tr><td colspan="7" align="center"><H1><?	getWeekBanner($Weeknum, "AFL")	?></H1></td>
						<td align="right"><a href="<? echo $PHP_SELF; ?>?Weeknum=<? echo $Weeknum-1; ?>" class="permLink">Prev</a> | <a href="<? echo $PHP_SELF; ?>?Weeknum=<? echo $Weeknum+1; ?>" class="permLink">Next</a> &nbsp; or &nbsp; 
							<B>Go to:</B>
							<?
								WeekDropDown($Weeknum)
							?>
						</td>
						</tr>
						</table>
						</form>
						<form method="POST" name="PicksForm" action="submitpicks.php" onSubmit="return checkPriority(this)">
						<table bgcolor=#FFFFFF callpadding="5" cellspacing="0" width="650" align="center">
						<?
						  echo "<tr><TH align=\"left\">Date</TH><Td colspan=\"8\" align=\"center\">".date("l F jS, Y g:i A", time() + (3600 * 3))." EST</td></tr>";
						?>
						<tr><TH colspan="2" width="100">Visitors</TH><TH>&nbsp;&nbsp;&nbsp;</TH><TH colspan="2" width="100">Home</TH><TH align="left" width="100">Game Time</TH><TH align="Left" width="100">Picks</TH><!--<TH align="Left" width="5">Priority</TH>--><TH align="Center" colspan="2" width="150">Winner</TH></tr>
						<?
						$CurrentDate = 20031022;
						$row = -1;
						$update = false;
						$gamesOver = true;
						
						do{
							if (($myrow["Away"] != NULL) && ($myrow["Away"] != ""))
							{
						//increment i for the row color
						$row++;
						$AwayTeam = $myrow["Away"];
						$HomeTeam = $myrow["Home"];
						echo "<tr bgcolor=\"".getRowColor($row)."\">";
						if ($myrow["GameDate"] == $CurrentDate) {
							//do nothing
						} 
						else
						{
							$CurrentDate = $myrow["GameDate"];
							echo "<td colspan=\"9\"><B><U>".date('l F jS, Y',strtotime($myrow["GameDate"]))."</U></B></td></tr>";
							$row++;
							echo "<tr bgcolor=\"".getRowColor($row)."\">";
						}//end if ($myrow["GameDate"] != $CurrentDate)
						echo "<td width=\"5\"><img src=\"http://football.joshhughes.com/AFL/images/small/".$myrow["Away"].".gif\" title=\"Click to select ".$myrow["Away"]."\" onClick=\"goSet(PicksForm.Game".$myrow["GameNum"].",'".$myrow["Away"]."')\"></td><td>".$myrow["Away"]."</td><td> @ </td>";
						echo "<td>".$myrow["Home"]."</td><td width=\"5\"><img src=\"http://football.joshhughes.com/AFL/images/small/".$myrow["Home"].".gif\" title=\"Click to select ".$myrow["Home"]."\" onClick=\"goSet(PicksForm.Game".$myrow["GameNum"].",'".$myrow["Home"]."')\"></td>";
						echo "<td>".date('g:i A',strtotime($myrow["GameTime"]))." EST</td>";
						if ($UserPickedrow["Week"] != "") 
							$update = true;
						 
						if (($UserPickedrow["Game".$myrow["GameNum"]] != "") && ($UserPickedrow["Game".$myrow["GameNum"]] != "-"))
						{//user has made a pick for this game.
							if ($UserPickedrow["Game".$myrow["GameNum"]] == $myrow["Winner"])
							{	//Game has ended and the user picked the correct winner.
								echo "<td><span style=\"color: #FF0000; font-weight: bold;\">".$myrow["Winner"]."</span></td>";
							}
							else
							{
								if (($myrow["PickEmailed"] != 0) || (date("Ymd",strtotime($myrow["GameDate"])) < date("Ymd")) || (date("Ymd",strtotime($myrow["GameDate"])) == date("Ymd")) && (date("Hi",strtotime($myrow["GameTime"])) < date("Hi",time() + (3600 * 3))))
								{	// Game is in progress, or has ended.  User cannot make pick anymore.
									echo "<td>".$UserPickedrow["Game".$myrow["GameNum"]]."</td>";
								}
								else
								{	//Game Is Not Being Played yet, so let the user change their pick.
									$gamesOver = false;
									echo "<td align=\"left\"><select name=\"Game".$myrow["GameNum"]."\" class=\"select\">";
									if($UserPickedrow["Game".$myrow["GameNum"]] == $myrow["Away"])
										echo "<option selected value=\"".$myrow["Away"]."\">".$myrow["Away"]."</option>";
									else
										echo "<option value=\"".$myrow["Away"]."\">".$myrow["Away"]."</option>";
						
									if($UserPickedrow["Game".$myrow["GameNum"]] == $myrow["Home"])
										echo "<option selected value=\"".$myrow["Home"]."\">".$myrow["Home"]."</option>";
									else
										echo "<option value=\"".$myrow["Home"]."\">".$myrow["Home"]."</option>";
						
									echo "</select></td>";
								}
							}//end if ($UserPickedrow["Game".$myrow["GameNum"]] == $myrow["Winner"])
						}
						else	//User has not picked, so give the user the options to pick from.
						{	
							if (($myrow["PickEmailed"] != 0) || (date("Ymd",strtotime($myrow["GameDate"])) < date("Ymd")) || (date("Ymd",strtotime($myrow["GameDate"])) == date("Ymd")) && (date("Hi",strtotime($myrow["GameTime"])) < date("Hi",time() + (3600 * 3))))
								echo "<td>In Progress</td>"; //if game is being played right now.  User cannot make pick anymore.
							else		//The game is not being played yet, let user pick.
								echo "<td align=\"left\"><select name=\"Game".$myrow["GameNum"]."\" class=\"select\" style=\"background-color: #FFFF99;\"><option selected value=\"\">Make Pick</option><option value=\"".$myrow["Away"]."\">".$myrow["Away"]."</option><option value=\"".$myrow["Home"]."\">".$myrow["Home"]."</option></select></td>";
							$gamesOver = false;
						}
						
						if ($myrow["Winner"] != "-")
							echo "<td><B>".$myrow["Winner"]."</B></td><td width=\"30\"><img src=\"http://football.joshhughes.com/AFL/images/small/".$myrow["Winner"].".gif\"></td></tr>";
						else
							echo "<td width=\"30\">&nbsp;</td><td>&nbsp;</td></tr>\n";
							
						}//end if (($myrow["Away"] != NULL) && ($myrow["Away"] != ""))
						}while($myrow = mysql_fetch_array($result));
						
						// Magic Number Row
						echo "<tr bgcolor=\"".getRowColor($row)."\"><td colspan=\"10\" align=\"center\" style=\"border-top: 1px solid #cb0000;\"><B>Magic Number: </B> <input type=\"text\" name=\"MagicNum\" value=\"".$UserPickedrow["MagicNum"]."\" size=\"3\" maxlength=\"3\" /><br/>(The total <i>combined</i> score of the <b>$AwayTeam</b> @ <b>$HomeTeam</b> game.)</td></tr>";
						
						// Check to see if USer is in Survivor Pool and display line for this.
						$IsSurviving = mysql_query("SELECT ID FROM AFLUsers WHERE ID=$userID AND Survivor='IN'") or die("Cannot query the database to see if user is in.<br/>");
						$myrow = mysql_fetch_array($IsSurviving);
						if($myrow["ID"] != "")
						{
						echo "
						<tr>
						<td colspan=\"9\"><hr color=\"#cb0000\" size=\"1\"/></td>
						</tr>
						<tr>
						<td colspan=\"9\" align=\"center\"><h4 align=\"center\">Survivor Picks</h4>";
						DisplayPicked($userID);
						echo "<br/>";
						DisplayAvailable($userID, $Weeknum);
						echo "<br/> Week $Weeknum survivor pick: ";
						SurvivorPickDropDown($userID,$Weeknum);
						echo "
						</td>
						</tr>";
						}
						
						// Print line of form buttons to submit the form if there are any games that can still be changed
						if (!$gamesOver)
						{
						echo "
						<tr>
						<td colspan=\"10\"><hr color=\"#cb0000\" size=\"1\"/></td>
						</tr>
						<tr>
						<td colspan=\"10\" align=\"center\">
						<input type=\"hidden\" name=\"UserID\" value=\"$userID\">
						<input type=\"hidden\" name=\"Week\" value=\"$Weeknum\">
						<input type=\"reset\" value=\"Reset Form\"> | <input type=\"Submit\" name=\"Submit\" value=\"";
						if($update)
						echo "Update Picks\">";
						else
						echo "Submit Picks\">";
						
						$results = mysql_query("SELECT UserID FROM AFLQuickPicks WHERE UserID = $userID") or die("Cannot query the database to see if user can use QuickPicks.<br/>");
						$myrow = mysql_fetch_array($results);
						if($myrow["UserID"] != "")
						echo " | <input type=\"Submit\" name=\"QP\" value=\"Quick Picks\" />";
						echo "</td></tr>";
						}
						echo "<tr>
						<td colspan=\"10\" align=\"center\">Want to try something new?  Don't just make your picks, <a href=\"weighpicks.php\" class=\"permLink\">weigh your picks</a>.</td>
						</tr>";
						echo "</table></form>";
					}
					else
					{
						?>
						<table bgcolor="#FFFFFF" border="0" width="100%">
							<tr>
								<td align="center">I'm sorry, you didn't make the playoffs.  You can log in to the site, but cannot make any more picks.  If you think there has been a mistake please contact the administrator though the suggestion box (in bottom left corner).</td>
							</tr>
						</table>
						<?
					}
					?>
				</td></tr></table>
			<!-- InstanceEndEditable -->
			
			</div>
			<div id="leftcol" >
				<?PHP include_once("layout/left_column.php"); ?>
			</div>
			
		</div>
		
		<div id="footer" >
			<?PHP include_once("layout/footer.html"); ?>
		</div>
</div>
</body>
<!-- InstanceEnd --></html>